package com.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.xiaomi.entity.Tuijian;
import com.xiaomi.model.Result;
import com.xiaomi.service.TuijianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TuijianController
 * @Description TODO
 * @Author zcy
 * @Date 2021/11/27 16:46
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/tuijian",produces = "application/json;charset=utf-8")
public class TuijianController {
    @Autowired
    private TuijianService tuijianService;
    @GetMapping("/list.do")
    @ResponseBody
    public String tuijian(@RequestParam Integer page, @RequestParam Integer limit,
                          @RequestParam(required = false) String tjName,
                          @RequestParam(required = false) BigDecimal price){
        Map<String,Object> pars = new HashMap();
        pars.put("tjName",tjName);
        pars.put("price",price);
        if (page!=null && limit !=null){
            pars.put("offset",(page-1)*limit);
            pars.put("limit",limit);
        }

        List<Tuijian> list = tuijianService.pageSearch(pars);
        Integer count = tuijianService.getCount(pars);

        Result result = new Result();
        result.setData(list);
        result.setCode(0);
        result.setCount(count);
        String json = JSON.toJSONString(result);
        return json;
    }
    @GetMapping("/add.do")
    @ResponseBody
    public  String insert(Tuijian tuijian){

            tuijianService.insert(tuijian);


        Result result = new Result();
        result.setCode(0);
        result.setMsg("添加成功");
        String json = JSON.toJSONString(result);
        return json;
    }
    @GetMapping("/update.do")
    @ResponseBody
    public  String update(Tuijian tuijian){

            tuijianService.update(tuijian);


        Result result = new Result();
        result.setCode(0);
        result.setMsg("修改成功");
        String json = JSON.toJSONString(result);
        return json;
    }
    @GetMapping("/del.do")
    @ResponseBody
    public String delete(@RequestParam Integer tjId){
        tuijianService.delete(tjId);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("删除成功");
        String json = JSON.toJSONString(result);
        return json;
    }
    @GetMapping("/sj.do")
    @ResponseBody
    public String sjupdate(Integer shangJia ,Integer tjId){
        tuijianService.sjupdate(shangJia, tjId);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("修改成功");
        String json = JSON.toJSONString(result);
        return json;
    }
}
